package pro.syj.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pro.syj.bean.Manager;
import pro.syj.db.DBassist;

public class ManagerDao {
	/*
	 * 查看所有管理员信息
	 * return ArrayList<Manager>
	 */
	public ArrayList<Manager> selectAll(){
	 	
		ArrayList<Manager> managerlist = new ArrayList<>();
		String sql = "select * from minfo";
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql);
			while(rss != null && rss.next()){
				Manager manager = new Manager();
				manager.setMid(rss.getString(1));
				manager.setPsd(rss.getString(2));
				managerlist.add(manager);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return managerlist;
	}
	
	/*
	 * (管理员登录)验证id和psd是否正确
	 * param mid psd
	 * return int 
	 */
	public int isValid(String mid,String psd){
		
		String sql = "select * from minfo where mid = ? and psd = ?";
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql, mid,psd);
			if(rss != null && rss.next()){
				return 1;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return -1;
	}
	
	/*
	 * 根据mid获取一个管理员
	 * param mid
	 * return Manager
	 */
	public Manager selectOne(String mid){
		
		String sql = "select * from minfo where mid = ?";
		Manager manager = new Manager();
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql, mid);
			if(rss != null && rss.next()){
				manager.setMid(rss.getString(1));
				manager.setPsd(rss.getString(2));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return manager;
	}
}
